package org.example.ideavr.config;

import lombok.extern.slf4j.Slf4j;
import org.example.ideavr.common.json.JsonObjectMapper;
import org.example.ideavr.interception.JwtTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


/**
 * Web MVC 配置类
 */

@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

     public void addInterceptors(InterceptorRegistry registry){
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns()
                .excludePathPatterns("/**");
    }


// 配置消息转换器 (时间格式化)

    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("开始配置消息转换器...");
        //创建对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器 jackson实现java->json
        converter.setObjectMapper(new JsonObjectMapper());
        //添加到消息转换器列表
        converters.add(converter);
    }


}

